/**
 * 发送GET异步请求
 * @param {string} url 
 * @param {string} params 
 * @param {function} callback 
 * @param {object} headers 
 * @param {string} dataType 
 */
function get(url,params,callback,headers={},dataType='json'){
    return new Promise((resolve,reject)=>{
        const xhr=new XMLHttpRequest
        xhr.onreadystatechange=()=>{
            if(xhr.readystate===4){
                if(xhr.status===200){
                    let res=JSON.parse(xhr.responseText)
                    resolve(res)
                    switch(dataType){
                        case 'json':
                        res=JSON.parse(res)
                        break;
    
                    default:
                        break;
                    }
    
                    callback(res)
                }else{
                    reject(xhr.status);
                    }
            }
        }
        xhr.open('get',`${url}?${params}`)
    
        for(let key in headers)
        {
            xhr.setRequestHeader(key,header[key])
        }
        xhr.send(null)
    })
    
}





/**
 * 发送POST异步请求
 * @param {string}   url 
 * @param {string}   params 
 * @param {function} callback 
 * @param {object}   headers 
 * @param {string}   dataType 
 */
function get(url,params,callback,headers={},dataType='json'){
    return new Promise((resolve,reject)=>{
        const xhr=new XMLHttpRequest
        xhr.onreadystatechange=()=>{
            if(xhr.readystate===4){
                if(xhr.status===200){
                    let res=JSON.parse(xhr.responseText)
                    resolve(res)
                    switch(dataType){
                        case 'json':
                        res=JSON.parse(res)
                        break;

                    default:
                        break;
                    }

                    callback(res)
                }else{
                    reject(xhr.status);
                    }
            }
        }
        xhr.open('post',url)
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')

        for(let key in headers)
        {
            xhr.setRequestHeader(key,header[key])
        }
        xhr.send(params)
        })
    
}
















